//
//  UIView+MLKit.m
//
//  Created by Molon on 13/11/12.
//  Copyright (c) 2013 Molon. All rights reserved.
//

#import "UIView+MLKit.h"

@implementation UIView (MLKit)

- (BOOL)containsSubViewOfClassType:(Class)cls {
    for (UIView *view in [self subviews]) {
        if ([view isMemberOfClass:cls]) {
            return YES;
        }
    }
    return NO;
}

- (BOOL)isDescendantOfViewClass:(Class)cls
{
    UIView *superview = self.superview;
    while (superview) {
        if ([superview isKindOfClass:cls]) {
            return YES;
        }
        
        superview = superview.superview;
    }
    return NO;
}

- (void)removeAllSubViews {
    for (UIView *view in [self subviews]) {
        [view removeFromSuperview];
    }
}

- (void)removeSubviewsWithSubviewClass:(Class)cls
{
    for (UIView *view in [self subviews]) {
        if ([view isKindOfClass:cls]) {
            [view removeFromSuperview];
        }
    }
}

- (CGPoint)frameOrigin {
	return self.frame.origin;
}

- (void)setFrameOrigin:(CGPoint)newOrigin {
	self.frame = CGRectMake(newOrigin.x, newOrigin.y, self.frame.size.width, self.frame.size.height);
}

- (CGSize)frameSize {
	return self.frame.size;
}

- (void)setFrameSize:(CGSize)newSize {
	self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y,
							newSize.width, newSize.height);
}

- (CGFloat)frameX {
	return self.frame.origin.x;
}

- (void)setFrameX:(CGFloat)newX {
	self.frame = CGRectMake(newX, self.frame.origin.y,
							self.frame.size.width, self.frame.size.height);
}

- (CGFloat)frameY {
	return self.frame.origin.y;
}

- (void)setFrameY:(CGFloat)newY {
	self.frame = CGRectMake(self.frame.origin.x, newY,
							self.frame.size.width, self.frame.size.height);
}

- (CGFloat)frameRight {
	return self.frame.origin.x + self.frame.size.width;
}

- (void)setFrameRight:(CGFloat)newRight {
	self.frame = CGRectMake(newRight - self.frame.size.width, self.frame.origin.y,
							self.frame.size.width, self.frame.size.height);
}

- (CGFloat)frameBottom {
	return self.frame.origin.y + self.frame.size.height;
}

- (void)setFrameBottom:(CGFloat)newBottom {
	self.frame = CGRectMake(self.frame.origin.x, newBottom - self.frame.size.height,
							self.frame.size.width, self.frame.size.height);
}

- (CGFloat)frameWidth {
	return self.frame.size.width;
}

- (void)setFrameWidth:(CGFloat)newWidth {
	self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y,
							newWidth, self.frame.size.height);
}

- (CGFloat)frameHeight {
	return self.frame.size.height;
}

- (void)setFrameHeight:(CGFloat)newHeight {
	self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y,
							self.frame.size.width, newHeight);
}


- (CGRect)midFrameWithHeight:(CGFloat)height width:(CGFloat)width
{
    return CGRectMake((self.frameWidth-width)/2, (self.frameHeight-height)/2, width, height);
}


+ (UINib *)nib
{
    return [UINib nibWithNibName:NSStringFromClass([self class]) bundle:[NSBundle mainBundle]];
}

+ (instancetype)instanceFromNib
{
    return [[[NSBundle mainBundle]loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil]lastObject];
}


- (void)simpleTransitionWithDuration:(CFTimeInterval)duration andType:(NSString*)type
{
    CATransition *animation = [CATransition animation];
    animation.duration = duration;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    animation.type = type;
    [self.layer addAnimation:animation forKey:nil];
}

#pragma mark - smooth corner
- (void)setLayerBackgroundColor:(UIColor*)color
{
    self.backgroundColor = [UIColor clearColor];
    self.layer.backgroundColor = color.CGColor;
}

- (void)setLayerCornerRadius:(CGFloat)radius
{
    self.layer.cornerRadius = radius;
    self.layer.masksToBounds = NO;
}

- (void)setLayerShouldRasterize:(BOOL)shouldRasterize
{
    if (shouldRasterize) {
        self.layer.rasterizationScale = [UIScreen mainScreen].scale;
    }
    self.layer.shouldRasterize = shouldRasterize;
}

- (void)activateMLSmoothCornerWithRadius:(CGFloat)radius andBackgroundColor:(UIColor*)color andShouldRasterize:(BOOL)shouldRasterize
{
    [self setLayerCornerRadius:radius];
    if (color) {
        [self setLayerBackgroundColor:color];
    }
    [self setLayerShouldRasterize:shouldRasterize];
}


#pragma mark - badge
- (UIView *)showBadgeValue:(NSString *)strBadgeValue
{
    return [self showBadgeValue:strBadgeValue offset:CGPointZero];
}

- (UIView *)showBadgeValue:(NSString *)strBadgeValue offset:(CGPoint)offset
{
    [self removeBadgeValue];
    
    UITabBar *tabBar = [[UITabBar alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
    UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"" image:nil tag:0];
    item.badgeValue = strBadgeValue;
    tabBar.items = @[item];
    //寻找
    for (UIView *viewTab in tabBar.subviews) {
        for (UIView *subview in viewTab.subviews) {
            NSString *strClassName = [NSString stringWithUTF8String:object_getClassName(subview)];
            if ([strClassName isEqualToString:@"UITabBarButtonBadge"] ||
                [strClassName isEqualToString:@"_UIBadgeView"]) {
                //从原视图上移除
                [subview removeFromSuperview];
                //添加到新视图右上角
                [self.superview addSubview:subview];
                subview.frame = CGRectMake(self.frame.origin.x+self.frame.size.width-subview.frame.size.width/2+offset.x, self.frame.origin.y-4+offset.y,subview.frame.size.width, subview.frame.size.height);
                return subview;
            }
        }
    }
    return nil;
}

- (void)removeBadgeValue
{
    for (UIView *subview in self.superview.subviews) {
        NSString *strClassName = [NSString stringWithUTF8String:object_getClassName(subview)];
        if ([strClassName isEqualToString:@"UITabBarButtonBadge"] ||
            [strClassName isEqualToString:@"_UIBadgeView"]) {
            [subview removeFromSuperview];
            break;
        }
    }
}

@end
